/**
 * job管理初始化
 */
var job = {
		id: "jobTable",	//表格id
		seItem: null,		//选中的条目
		table: null,
		layerIndex: -1
};

/**
 * 初始化表格的列
 */
job.initColumn = function () {
	return [
		{field: 'selectItem', radio: true},
		{title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
		{title: '任务名称', field: 'jobName', align: 'center', valign: 'middle', sortable: true},
		{title: 'cron', field: 'cron', align: 'center', valign: 'middle', sortable: true},
		{title: '类名', field: 'springBeanName', align: 'center', valign: 'middle', sortable: true},
		{title: '方法名', field: 'methodName', align: 'center', valign: 'middle', sortable: true},
		{title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true,formatter: operateFormatter},
		{title: '创建人', field: 'loginName', align: 'center', valign: 'middle', sortable: false},
		{title: '创建时间', field: 'create_date', align: 'center', valign: 'middle', sortable: true}


		];
};
function operateFormatter(value,row,index) 
//row 获取这行的值 ，index 获取索引值 
{ 

	if(value==0){
		return '停止';
	}else{
		return '正常';
	}
}
/**
 * 检查是否选中
 */
job.check = function () {
	var selected = $('#' + this.id).bootstrapTable('getSelections');
	if(selected.length == 0){
		EA.info("请先选中表格中的某一记录！");
		return false;
	}else{
		job.seItem = selected[0];
		return true;
	}
};

/**
 * 点击添加job
 */
job.openAddjob = function () {
	var index = layer.open({
		type: 2,
		title: '添加任务',
		area: ['100%', '100%'], //宽高
		fix: true, //不固定
		maxmin: true,
		content: '/job/form?type=add'
	});
	this.layerIndex = index;
};

/**
 * 打开查看job详情
 */
job.openjobDetail = function () {
	if (this.check()) {
		var index = layer.open({
			type: 2,
			title: '任务详情',
			area: ['100%', '100%'], //宽高
			fix: true, //不固定
			maxmin: true,
			content:'/job/form?type=update&id=' +job.seItem.id
		});
		this.layerIndex = index;
	}
};

/**
 * 删除job
 */
job.delete = function () {
	if (this.check()) {



		var operation = function(){
			var ajax = new $ax( "/job/delete?jobName="+job.seItem.jobName, function (data) {
				EA.success("删除成功!");
				job.table.refresh();
			}, function (data) {
				EA.error("删除失败!" + data.responseJSON.message + "!");
			});
			ajax.set("id",job.seItem.id);
			ajax.start();
		};

		EA.confirm("是否刪除该任务?", operation);
	}
};

/**
 * 一键开启
 */
job.startjob = function () {
	if (this.check()) {



		var operation = function(){
			var ajax = new $ax( "/job/start?jobName="+job.seItem.jobName, function (data) {
				EA.success("开启成功!");
				job.table.refresh();
			}, function (data) {
				EA.error("开启失败!" + data.responseJSON.message + "!");
			});
			ajax.set("id",job.seItem.id);
			ajax.start();
		};

		EA.confirm("是否开启该任务?", operation);
	}
};
/**
 * 一键关闭
 */
job.endjob = function () {
	if (this.check()) {



		var operation = function(){
			var ajax = new $ax( "/job/end?jobName="+job.seItem.jobName, function (data) {
				EA.success("停止成功!");
				job.table.refresh();
			}, function (data) {
				EA.error("停止失败!" + data.responseJSON.message + "!");
			});
			ajax.set("id",job.seItem.id);
			ajax.start();
		};

		EA.confirm("是否停止该任务?", operation);
	}
};




/**
 * 查询job列表
 */
job.search = function () {
	var queryData = {};
	queryData['jobName'] = $("#condition").val();
	job.table.refresh({query: queryData});
};

$(function () {
	var defaultColunms = job.initColumn();
	var table = new BSTable(job.id, "/job/list", defaultColunms);
	table.setPaginationType("server");
	job.table = table.init();
});
